using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ServerNET.Server;

namespace ServerNET.Server
{
    public class CLog
    {
        // Print console string
        public static void Trace(String s)
        {
            if (CGlobal.Server.Debug)
            {
                Console.WriteLine(DateTime.Now + "> " + s);
            }
            //SaveLog(CGlobal.Config.LogTrace + CCommon.GetCurrDateString() + ".log", DateTime.Now + "> " + s);
        }

        // Get Current Time
        public static string GetCurrTimeString()
        {
            DateTime dt = DateTime.Now;
            return String.Format("{0:d/M/yyyy HH:mm:ss}", dt);
        }

        // Save Log
        public static void SaveLog(string FileName, string Content)
        { 
	        string s = GetCurrTimeString();
	        s += "|";
	        s += Content;

	        try
	        {
                StreamWriter w = File.AppendText(FileName);
                if (w != null)
                {
                    w.WriteLine(s);
                }
                w.Close();
	        }
	        catch
	        {
        		
	        }            
        }

        // Log Error
        public static void LogError(String Error)
        {
            SaveLog(CGlobal.Config.LogError + "Error.log", Error);
            Trace(Error);
        }

        public static void LogClientResult(string UserName, int GameId, int TableId, int NumPlayer, double GoldBet, double GoldAdd, double Gold)
        {
            //CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString2;
            //CSQL.SaveData("insert into LogClientResult(UserName, GameId, TableId, NumPlayer, GoldBet, GoldAdd, Gold, Time) values" +
            //    "('" + UserName + "'," + GameId + "," + TableId + "," + NumPlayer + "," + GoldBet + "," + GoldAdd + "," + Gold + ",GETDATE())");
            //CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString;

            string Path, Content;
            Path = CGlobal.Config.LogEvent + "LogClientResult.log";
            Content = UserName + "|" + GameId + "|" + TableId + "|" + NumPlayer + "|" + GoldBet + "|" + GoldAdd + "|" + Gold;
            SaveLog(Path, Content);
        }

        public static void LogCCU(int CCU)
        {
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString2;
            CSQL.SaveData("insert into LogCCU(CCU, Time) values(" + CCU + ",GETDATE())");
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString;
        }

        public static void LogSystem(string Content)
        {
            SaveLog(CGlobal.Config.LogSystem, Content);
        }

        public static void LogExchangeXu(string UserName, double XuEx, double GoldEx, double Xu, double Gold)
        {
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString2;
            CSQL.SaveData("insert into LogExchange(UserName, XuEx, GoldEx, Xu, Gold, Time) values('"+UserName+"',"+XuEx+","+GoldEx+","+Xu+","+Gold+",GETDATE())");
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString;
        }

        public static void LogBuyMarketItem(string UserName, double XuAdd, double GoldAdd, double XuTax, double GoldTax, double Xu, double Gold)
        {
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString2;
            CSQL.SaveData("insert into LogBuyMarketItem(UserName, XuAdd, GoldAdd, XuTax, GoldTax, Xu, Gold, Time) values"+
                "('" + UserName + "'," + XuAdd + "," + GoldAdd + "," + XuTax + "," + GoldTax + ","+Xu+","+Gold+",GETDATE())");
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString;
        }

        public static void LogMoney(string Action, string UserName, double Xu, double Gold, double XuAdd, double GoldAdd)
        {
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString2;
            CSQL.SaveData("insert into LogMoney(Action, UserName, Xu, Gold, XuAdd, GoldAdd, Time) values" +
                "('" + Action + "','" + UserName + "'," + Xu + "," + Gold + "," + XuAdd + "," + GoldAdd + ",GETDATE())");
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString;
            //string Path, Content;
            //Path = CGlobal.Config.LogEvent + "LogMoney.log";
            //Content = Action + "|" + UserName + "|" + Xu + "|" + Gold + "|" + XuAdd + "|" + GoldAdd;
            //SaveLog(Path, Content);
        }

        public static void LogMoney2(string Action, string UserName, double Xu, double Gold, double XuAdd, double GoldAdd)
        {
            //CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString2;
            //CSQL.SaveData("insert into LogMoney(Action, UserName, Xu, Gold, XuAdd, GoldAdd, Time) values" +
            //    "('" + Action + "','" + UserName + "'," + Xu + "," + Gold + "," + XuAdd + "," + GoldAdd + ",GETDATE())");
            //CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString;
            string Path, Content;
            Path = CGlobal.Config.LogEvent + "LogMoney.log";
            Content = Action + "|" + UserName + "|" + Xu + "|" + Gold + "|" + XuAdd + "|" + GoldAdd;
            SaveLog(Path, Content);
        }

        public static void LogBuyShopItem(string UserName, int ItemId, int ItemType, double GoldAdd, double XuAdd, double VKim, double Xu)
        {
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString2;
            CSQL.SaveData("insert into LogBuyShopItem(UserName, ItemId, ItemType, GoldAdd, XuAdd, Gold, Xu, Time) values" +
                "('" + UserName + "'," + ItemId + "," + ItemType + ","+GoldAdd+","+XuAdd+","+VKim+","+Xu+",GETDATE())");
            CSQL.CONNECTION_STRING = CGlobal.Config.ConnectionString;
        }
    }
}
